package com.example.smartcity_poverty.ui.AllService.ActivityManAge.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.ui.AllService.ActivityManAge.ManAgeViewModel
import com.example.smartcity_poverty.ui.AllService.ActivityManAge.adapter.ActManAgeListAdapter
import kotlinx.android.synthetic.main.fragment_activity_man_age.*

/**
 * 活动列表
 */
class ActivityManAgeFragment : Fragment() {

    private val vm by lazy {
        ViewModelProviders.of(this)[ManAgeViewModel::class.java]
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_activity_man_age, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 填充数据
        setListAdapter(HId)
    }

    private fun setListAdapter(hId: Int) {
        vm.setHList(hId)
        vm.hLiveData.observe(this, Observer { resources ->
            resources.getOrNull()?.apply {
                if (this.code == 200){
                    vm.hListList.clear()
                    vm.hListList.addAll(this.rows!!)

                    // 填充数据
                    ManAgeListRv.apply {
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = ActManAgeListAdapter(vm.hListList, R.layout.re_activity_manage_list, vm.hListList.size)
                    }
                }
            }
        })
    }

    companion object{
        var HId = 0
    }
}